package com.service.impl;

import com.constant.Constant;
import com.dao.UserDao;
import com.entity.GoldPoints;
import com.entity.User;
import com.exception.ServiceException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.UserService;
import com.util.AjaxResult;
import com.vo.UserBackVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

/**
 * date:2021/4/29
 * description:
 * Author:Stargazer
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User loginBack(User user) {
        System.out.println(user);
        User u = userDao.selectByLoginnameAndPassword(user);
        if("normal".equals(u.getRole())||u == null){
            throw new ServiceException("用户名/密码错误");
        }
        u.setLogin_date(new Date());
        userDao.loginFront(u);
        return u;
    }


    @Override
    public PageInfo<UserBackVo> findBackUser(Integer page,UserBackVo userBackVo) {
        System.out.println(userBackVo);
        List<UserBackVo> list=null;
        try {
            PageHelper.startPage(page, Constant.PAGE_SIZE);
            list = userDao.findBackUser(userBackVo);
        }catch (Exception e){
            e.printStackTrace();
        }
        return new PageInfo<UserBackVo>(list);
    }

    @Override
    public AjaxResult showBackUser(Integer id) {
        AjaxResult ajaxResult=new AjaxResult();
        ajaxResult.setObj(userDao.showBackUser(id));
        return ajaxResult;
    }

    @Override
    public void modifyBackUser(User user) {
        userDao.modifyBackUser(user);
    }

    @Override
    public void modifyBackUserS(User user) {
        userDao.modifyBackUserS(user);
    }

    @Override
    public User selectDetailGPById(int id) throws ServiceException {
        User user = userDao.selectDetailGPById(id);
        //计算金币/积分总数量；
        Set<GoldPoints> gps = user.getGps();
        //遍历HashSet
        Iterator<GoldPoints> iterator=gps.iterator();
        int gold_count=0;
        int point_count=0;
        while (iterator.hasNext()){
            GoldPoints gp = iterator.next();
            gold_count += gp.getGold_count();
            point_count += gp.getPoint_count();
        }
        user.setGold_count(gold_count);
        user.setPoint_count(point_count);
        return user;
    }




    //罗前台checkEmail
    @Override
    public List<User> check(String login_name) {
        return userDao.check(login_name);
    }
    @Override
    public List<User> checkEmail(String email) {
        return userDao.checkEmail(email);
    }
    @Override
    public User loginFront(User user) {
        System.out.println(user);
        User u = userDao.selectByLoginnameAndPassword(user);
        if("admin".equals(u.getRole())||u == null){
            throw new ServiceException("用户名/密码错误");
        }
        u.setLogin_date(new Date());
        userDao.loginFront(u);
        return u;
    }
    @Override
    public User regist(User user) {
        return userDao.regist(user);
    }
    @Override
    public String modifyUser(Integer id,String nickname,String email,String newPassword) {
        try {
            userDao.modifyUser(id,nickname,email,newPassword);
            return "修改成功";
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    @Override
    public void updateLoginDate(GoldPoints goldPoints) {
        userDao.updateLoginDate(goldPoints);
    }
    @Override
    public GoldPoints findLoginDate(Integer user_id) {
        return userDao.findLoginDate(user_id,"签到");
    }


}
